﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;
using System.Web.Routing;

namespace Wicresoft.SMS.Web.Controllers
{
    public class SendController : Controller
    {
        //
        // GET: /Send/

        public ActionResult Index(AddressSendModle Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IAddressWebDataAccess dataAccess = new sqlWebAddressDataAccess(configProvider.ConnectionString);
            //TODO UserID必须得到当前用户
            Model.Infos = dataAccess.GetNumberByUsers(2);
            return View(Model);
        }

        //转发
        public ActionResult forwardmessage(AddressSendModle Model)
        {
            return View(Model);
        }

        //发送短信
        public ActionResult Send(string addresslist, string addresslists) 
        {
            if (addresslist == "")
            {
                TempData["msg"] = "请输入短信收件人";
                return Redirect("index");
            }
            if (addresslists == "")
            {
                TempData["msg"] = "请输入短信内容";
                return Redirect("index");
            }
            else
            {
                WebConfigProvider configProvider = new WebConfigProvider();
                ISendWebDataAccess dataAccess = new sqlWebSendDataAccess(configProvider.ConnectionString);
                //TODO UserName必须得到当前用户
                int result = dataAccess.SendSms(addresslist, addresslists, "Admin");
                //根据返回值判断是否短信成功放入数据库
                if (result == 1)
                {
                    TempData["msg"] = "错误的用户名";
                }
                else if (result == 2)
                {
                    TempData["msg"] = "错误的联系人名单或电话号码 ";
                }
                else if (result == 3)
                {
                    TempData["msg"] = "本月信息已发完";
                }
                else if (result == 4)
                {
                    TempData["msg"] = "其他错误";
                }
                else if (result == 0)
                {
                    TempData["msg"] = "短信成功放入数据库";
                }
                else
                {
                    TempData["msg"] = result;
                }
                return Redirect("index");
            }
        }

    }
}
